package com.twu.biblioteca.factory;

import com.twu.biblioteca.BibliotecaApp;
import com.twu.biblioteca.Receiver;
import com.twu.biblioteca.Println;
import com.twu.biblioteca.Shelf;

public class ReturnAction extends BasicAction {
    private ReturnAction() {
    }

    private static ReturnAction returnAction = null;

    public synchronized static ReturnAction getInstance() {
        if (returnAction == null) {
            returnAction = new ReturnAction();
        }
        return returnAction;
    }

    @Override
    public void run(BibliotecaApp bibliotecaApp, Receiver receiver) {
        Shelf bookShelf = bibliotecaApp.getBookShelf();
        String returnBookName = inputValidReturnBookName(receiver, bookShelf);
        if (bookShelf.isBorrowed(returnBookName)) {
            removeBookBorrower(returnBookName, bookShelf);
            Println.println(Println.SUCCEED_RETURN_BOOK);
        } else {
            Println.println(Println.FAIL_RETURN_BOOK);
        }
    }

    private void removeBookBorrower(String bookName, Shelf shelf) {
        shelf.returnGoods(bookName);
    }
}
